package com.dm.wallpaper.library.helpers;

public class URLUtil {

    private static final String LOGTAG = "webkit";
    private static final boolean TRACE = false;

    static final String ASSET_BASE = "file:///ohos_asset/";
    static final String RESOURCE_BASE = "file:///ohos_res/";
    static final String FILE_BASE = "file:";
    static final String PROXY_BASE = "file:///cookieless_proxy/";
    static final String CONTENT_BASE = "content:";


    /**
     * if the url is valid
     * @param url the string to check
     * @return {@code true} if the url is valid.
     */
    public static boolean isValidUrl(String url) {
        if (url == null || url.length() == 0) {
            return false;
        }

        return (isAssetUrl(url) ||
                isResourceUrl(url) ||
                isFileUrl(url) ||
                isAboutUrl(url) ||
                isHttpUrl(url) ||
                isHttpsUrl(url) ||
                isJavaScriptUrl(url) ||
                isContentUrl(url));
    }

    /**
     * if the url is an asset file
     * @param url the string to check
     * @return {@code true} if the url is an asset file.
     */
    public static boolean isAssetUrl(String url) {
        return (null != url) && url.startsWith(ASSET_BASE);
    }


    /**
     * if the url is a resource file
     * @param url the string to check
     * @return {@code true} if the url is a resource file.
     * @hide
     */
    public static boolean isResourceUrl(String url) {
        return (null != url) && url.startsWith(RESOURCE_BASE);
    }


    /**
     * if the url is a local file
     * @param url the string to check
     * @return {@code true} if the url is a local file.
     */
    public static boolean isFileUrl(String url) {
        return (null != url) && url.startsWith(FILE_BASE)
                && !url.startsWith(ASSET_BASE)
                && !url.startsWith(PROXY_BASE);
    }


    /**
     * is About Url
     * @param url url the string to check
     * @return {@code true} if the url is an about: url.
     */
    public static boolean isAboutUrl(String url) {
        return (null != url) && url.startsWith("about:");
    }

    /**
     *  if the url is a data: url
     * @param url the string to check
     * @return {@code true} if the url is a data: url.
     */
    public static boolean isDataUrl(String url) {
        return (null != url) && url.startsWith("data:");
    }

    /**
     * if the url is a javascript: url
     * @param url the string to check
     * @return {@code true} if the url is a javascript: url.
     */
    public static boolean isJavaScriptUrl(String url) {
        return (null != url) && url.startsWith("javascript:");
    }


    /**
     * if the url is an http: url
     * @param url the string to check
     * @return {@code true} if the url is an http: url.
     */
    public static boolean isHttpUrl(String url) {
        return (null != url) &&
                (url.length() > 6) &&
                url.substring(0, 7).equalsIgnoreCase("http://");
    }

    /**
     * if the url is an https: url
     * @param url the string to check
     * @return {@code true} if the url is an https: url.
     */
    public static boolean isHttpsUrl(String url) {
        return (null != url) &&
                (url.length() > 7) &&
                url.substring(0, 8).equalsIgnoreCase("https://");
    }


    /**
     * if the url is a content: url
     * @param url the string to check
     * @return {@code true} if the url is a content: url.
     */
    public static boolean isContentUrl(String url) {
        return (null != url) && url.startsWith(CONTENT_BASE);
    }

}
